﻿using M_ApiResult;
using M_Bom.API.Application.Command.BomCommand;
using M_Bom_Domain;
using M_Bom_Domain.Dto;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M_Bom.API.Controllers
{
    /// <summary>
    /// Bom控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BomController : ControllerBase
    {
        protected readonly IMediator mediator;

        public BomController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<Bom>>> Show([FromQuery] BomShowCommand command)
        {
            ApiResult<PageDto<Bom>> result = new ApiResult<PageDto<Bom>>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 添加Bom
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddBom(BomCreacteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
      
        /// <summary>
        /// 删除Bom
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelBom(BomDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 反填原料id
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<BomYGuan>>> BackfillYuan([FromQuery] BomBackfillCommand command)
        {
            ApiResult<List<BomYGuan>> result = new ApiResult<List<BomYGuan>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
        /// <summary>
        /// 反填Bom
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Bom>> BackfillBom([FromQuery] BomBackfillBomCommand command)
        {
            ApiResult<Bom> result = new ApiResult<Bom>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateBom(BomUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {

                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;

        }
    }
}
